package com.baidu.shoppingmall.comment.service.impl;

import com.baidu.shoppingmall.comment.constant.*;
import com.baidu.shoppingmall.comment.dao.CommentInfoDAO;
import com.baidu.shoppingmall.comment.domain.CommentInfoPO;
import com.baidu.shoppingmall.comment.domain.CommentInfoDTO;
import com.baidu.shoppingmall.comment.service.CommentInfoService;
import com.baidu.shoppingmall.common.util.DateUtils;
import com.baidu.shoppingmall.order.domain.OrderInfoDTO;
import com.baidu.shoppingmall.order.domain.OrderItemDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 评论信息管理模块的service
 * @author yjf
 *
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoDAO,CommentInfoPO> implements CommentInfoService {
	
	private static final Logger logger = LoggerFactory.getLogger(CommentInfoServiceImpl.class);

	/**
	 * 评论信息管理模块的DAO
	 */
	@Autowired
	private CommentInfoDAO commentInfoDAO;
	
	/**
	 * 新增评论信息
	 * @param commentInfoDTO 评论信息DTO对象
	 */
	@Override
	public Boolean saveCommentInfo(CommentInfoDTO commentInfoDTO) {
		try {
			// 计算评论的总分数
			Integer totalScore = Math.round((commentInfoDTO.getGoodsScore() 
					+ commentInfoDTO.getCustomerServiceScore() 
					+ commentInfoDTO.getLogisticsScore()) / 3); 
			commentInfoDTO.setTotalScore(totalScore); 

			// 设置是否为默认评论
			commentInfoDTO.setDefaultComment(DefaultComment.NO);
			
			// 设置评论的状态
			commentInfoDTO.setCommentStatus(CommentStatus.APPROVING);
			
			// 设置评论的类型
			Integer commentType = 0;
			if(totalScore >= 4) {
				commentType = CommentType.GOOD_COMMENT;
			} else if(totalScore == 3) {
				commentType = CommentType.MEDIUM_COMMENT;
			} else if(totalScore > 0 && totalScore <= 2) {
				commentType = CommentType.BAD_COMMENT;
			}
			
			commentInfoDTO.setCommentType(commentType);
			
			// 设置创建时间和修改时间
			commentInfoDTO.setCreateTime(new Date());
			commentInfoDTO.setUpdateTime(new Date());
			
			// 将评论信息保存到数据库中去
			CommentInfoPO commentInfoPO = commentInfoDTO.clone(CommentInfoPO.class);
			commentInfoDAO.insert(commentInfoPO);
			
			// 设置评论信息的id
			commentInfoDTO.setId(commentInfoPO.getId());
		} catch (Exception e) {
			logger.error("error", e);
			throw new RuntimeException("添加评论失败");
		}
		return true;
	}

	/**
	 * 新增自动发表的评论信息
	 * @param orderInfoDTO 订单信息DTO对象
	 * @param orderItemDTO 订单条目DTO对象
	 * @return 处理结果
	 */
	@Override
	public CommentInfoDTO saveAutoPublishedCommentInfo(
			OrderInfoDTO orderInfoDTO, OrderItemDTO orderItemDTO) {
		CommentInfoDTO commentInfoDTO = null;
		try {
			commentInfoDTO = createAutoPublishedCommentInfoDTO(orderInfoDTO, orderItemDTO);
			CommentInfoPO commentInfoDO = commentInfoDTO.clone(CommentInfoPO.class);
			commentInfoDAO.insert(commentInfoDO);

			//自动新增评论后  将评论信息表中的id传入(上面的新增与数据库建立了关系，即使不查询对象数据，也能获取到当前对象在数据库中生成的id)
			commentInfoDTO.setId(commentInfoDO.getId());
		} catch (Exception e) {
			logger.error("error", e);
			return null;
		}
		return commentInfoDTO;
	}


	/**
	 * 创建评论信息DTO对象
	 * @param orderInfoDTO 订单信息DTO对象
	 * @param orderItemDTO 订单条目DTO对象
	 * @return 评论信息DTO对象
	 */
	private CommentInfoDTO createAutoPublishedCommentInfoDTO(
			OrderInfoDTO orderInfoDTO, OrderItemDTO orderItemDTO) throws Exception {
		CommentInfoDTO commentInfoDTO = new CommentInfoDTO();

		commentInfoDTO.setUserAccountId(orderInfoDTO.getUserAccountId());
		commentInfoDTO.setUsername(orderInfoDTO.getUsername());
		commentInfoDTO.setOrderInfoId(orderInfoDTO.getId());
		commentInfoDTO.setOrderItemId(orderItemDTO.getId());
		commentInfoDTO.setGoodsId(orderItemDTO.getGoodsId());
		commentInfoDTO.setGoodsSkuId(orderItemDTO.getGoodsSkuId());
		commentInfoDTO.setGoodsSkuSaleProperties(orderItemDTO.getSaleProperties());
		commentInfoDTO.setTotalScore(CommentInfoScore.FIVE);
		commentInfoDTO.setGoodsScore(CommentInfoScore.FIVE);
		commentInfoDTO.setCustomerServiceScore(CommentInfoScore.FIVE);
		commentInfoDTO.setLogisticsScore(CommentInfoScore.FIVE);
		commentInfoDTO.setCommentContent(CommentContent.DEFAULT);
		commentInfoDTO.setShowPictures(ShowPictures.NO);
		commentInfoDTO.setDefaultComment(DefaultComment.YES);
		commentInfoDTO.setCommentStatus(CommentStatus.APPROVED);
		commentInfoDTO.setCommentType(CommentType.GOOD_COMMENT);
		commentInfoDTO.setCreateTime(DateUtils.getCurrentTime());
		commentInfoDTO.setUpdateTime(DateUtils.getCurrentTime());

		return commentInfoDTO;
	}
}
